package com.gitee.ly;

import com.gitee.ly.nami.NamiConfigurationLocalFix;
import org.noear.nami.NamiConfigurationDefault;
import org.noear.solon.cloud.CloudManager;
import org.noear.solon.core.AppContext;
import org.noear.solon.core.Plugin;

import java.net.UnknownHostException;

public class LightRegistryPluginImp implements Plugin {

    @Override
    public void start(AppContext context) throws UnknownHostException {
        //加载Light-Registry配置
        LightRegistryProperties properties = new LightRegistryProperties(context);
        if (properties.isEnable()) {
            ClientManage clientManage = new ClientManage(properties.getServerBaseUrl(),
                    properties.getServiceName(),
                    properties.getSchema(),
                    properties.getIp(),
                    properties.getPort(),
                    properties.getHeaderU());
            context.wrapAndPut(ClientManage.class, clientManage);
            //onMissingBean = DiscoverAndLoadBalancer.class
            context.beanMake(DefaultDiscoverAndLoadBalancer.class);
            CloudManager.register(new CloudDiscoveryServiceLightRegistryImpl(context));
        } else {
            ClientManage clientManage = new ClientManage(properties.getServerBaseUrl(),
                    properties.getServiceName(),
                    properties.getSchema(),
                    properties.getIp(),
                    properties.getPort(),
                    properties.getHeaderU(),
                    true);
            context.wrapAndPut(ClientManage.class, clientManage);
            NamiConfigurationDefault.proxy = new NamiConfigurationLocalFix(context);
        }
    }
}

